/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.proescola.dao;

import br.com.proescola.modelo.Aluno;
import br.com.proescola.modelo.Historico;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Joao
 */
public class HistoricoDAO {

    private Connection conexao;
    //private PreparedStatement stmt;

    public HistoricoDAO() throws SQLException {
        this.conexao = CriaConexao.getConexao(); //Istancia um objeto Connection, que é o parâmetro da classe CriaConexao.java
    }

    public void cadastrar(Historico h1) throws SQLException {
        //Prepara a estrutura da Query e declara o objeto PreparedStatement
        String sql = "INSERT INTO historico (cd_aluno_cd, cd_curso_cd, cd_disciplina_cd, p1, p2, nt, nr_bimestre, ano) VALUES (?,?,?,?,?,?,?,?)";
        PreparedStatement stmt = conexao.prepareStatement(sql);
        String ret = "erro";
        //Seta os valores que vão ser inseridos na Query
        stmt.setInt(1, h1.getIdAluno());
        stmt.setInt(2, h1.getIdCurso());
        stmt.setInt(3, h1.getIdDisciplina());
        stmt.setInt(4, h1.getP1());
        stmt.setInt(5, h1.getP2());
        stmt.setInt(6, h1.getNota());
        stmt.setInt(7, h1.getBimestre());
        stmt.setInt(8, h1.getAno());
        //Executa a inserção SQL
        stmt.execute();
        stmt.close();

    }

    public List<Historico> getLista() throws SQLException {
        String sql = "SELECT * FROM aluno";
        PreparedStatement stmt = this.conexao.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();

        List<Historico> historicos = new ArrayList<Historico>();
        while (rs.next()) {
            Historico h1 = new Historico(rs.getInt("cd_historico"), rs.getInt("cd_aluno_cd"), rs.getInt("cd_curso_cd"), rs.getInt("cd_disciplina_cd"), rs.getInt("p1"), rs.getInt("p2"), rs.getInt("nt"), rs.getInt("nr_bimestre"), rs.getInt("ano"));
            historicos.add(h1);
        }
        rs.close();
        stmt.close();
        return historicos;
    }
}
